package com.bocai.yuweiwangluo.activity;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 修改地区界面
 */
public class UpdataAreaActivity extends BaseActivity implements View.OnClickListener,Toolbar.OnMenuItemClickListener
{

    @ViewInject(R.id.et_nickname)
    EditText area;
    private BaseBean baseBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private String sex;
    @Override
    protected int contentView()
    {
        return R.layout.activity_updata;
    }
    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        sex=getIntent().getStringExtra("sex");
        initToolbar("地区");
        mToolbar.inflateMenu(R.menu.menu_updata);
        initEvent();

    }

    @Override
    protected void initEvent()
    {
        super.initEvent();
        mToolbar.setOnMenuItemClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if(TextUtils.isEmpty(area.getText())){
            Toast.makeText(this,"你还未填写地区",Toast.LENGTH_SHORT).show();
        }else{

            MyOkHttpClient myOkHttpClient=new MyOkHttpClient(this,this);
//            myOkHttpClient.updatainfo(UrlData.BASEURL+UrlData.UPDATAINFO, SharePrefencesUtil.getUser_id(this),sex,area.getText().toString(),handler);
        }
        return true;
    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:

                        baseBean= gson.fromJson(result, BaseBean.class);
                        if(0==baseBean.getState()){
                            Toast.makeText(UpdataAreaActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent();
                            intent.putExtra("area",area.getText().toString());
                            setResult(RESULT_OK, intent);
                            UpdataAreaActivity.this.finish();

                        }else{
                            showError(baseBean.getMsg());
                        }
                        break;
                    case RESPONSE_FAILED:
                        showError(result);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };
}
